package com.controleFamiliar.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.controleFamiliar.pojo.Despesa;
import com.controleFamiliar.pojo.FontePagadora;
import com.controleFamiliar.util.HibernateUtil;

public class FontePagadoraDAO extends GenericDAO<Integer, Despesa>{
	
	public void salvarFontePagadora(FontePagadora fonte) throws Exception {
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			FontePagadora novaFonte = new FontePagadora();
			novaFonte = fonte;
			session.save(novaFonte);
			session.flush();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
	}
	
	public void removerFontePagadora(Integer codFontePagadora) throws Exception {
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			String hql = "Delete From FontePagadora where codFontePagadora = :codFontePagadora";
			Query query = session.createQuery(hql);
			query.setInteger("codFontePagadora", codFontePagadora);
			int result = query.executeUpdate();
			System.out.println("Rows affected: " + result);
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<FontePagadora> listarTodasFontePagadoras(){
		List<FontePagadora> lista = new ArrayList<FontePagadora>();
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			StringBuilder sb = new StringBuilder();
			sb.append("FROM FontePagadora");
			Query q = session.createQuery(sb.toString());
			lista = q.list();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
		return lista;
	}
	
	@SuppressWarnings("unchecked")
	public FontePagadora obter(Integer cod){
		FontePagadora fonte = new FontePagadora();
		try{
			Session session = HibernateUtil.getSessionFactory().openSession();
			StringBuilder sb = new StringBuilder();
			sb.append("FROM FontePagadora where codFontePagadora = :cod");
			Query q = session.createQuery(sb.toString());
			q.setInteger("cod", cod);
			fonte = (FontePagadora)q.uniqueResult();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{HibernateUtil.getSessionFactory().close();}catch(Exception e){}
		}
		return fonte;
	}
	
}
